package com.taichuan.selfcheck.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;


/**
 * Created by Oisny on 2016/7/1.
 */
public class PropertyPreference {
    private static final String PROPERTY_FILENAME = "TC_Self_Check_Sp";
    private static PropertyPreference propertyPreference;
    private SharedPreferences mSharedPreferences;
    private Context mApplicationContext;

    PropertyPreference() {
    }

    public PropertyPreference(Context context) {
        mApplicationContext = context;
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mApplicationContext);
    }

    public static PropertyPreference get() {
        if (propertyPreference == null) {
            synchronized (PropertyPreference.class) {
                if (propertyPreference == null) {
                    propertyPreference = new PropertyPreference();
                }
            }
        }
        return propertyPreference;
    }

    public void initPropertyPreference(Context context) {
        mApplicationContext = context;
        mSharedPreferences = context.getSharedPreferences(PROPERTY_FILENAME, Context.MODE_PRIVATE);
    }

    public void set(String key, String value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public void set(String[] keys, String[] values) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        for (int i = 0; i < keys.length; i++) {
            editor.putString(keys[i], values[i]);
        }
        editor.apply();
    }

    public void set(String[] keys, int[] values) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        for (int i = 0; i < keys.length; i++) {
            editor.putInt(keys[i], values[i]);
        }
        editor.apply();
    }

    public void set(String[] keys, float[] values) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        for (int i = 0; i < keys.length; i++) {
            editor.putFloat(keys[i], values[i]);
        }
        editor.apply();
    }

    public void set(String[] keys, boolean[] values) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        for (int i = 0; i < keys.length; i++) {
            editor.putBoolean(keys[i], values[i]);
        }
        editor.apply();
    }

    public void set(String key, int value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public void set(String key, float value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    public void set(String key, Boolean value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }



    public void remove(String key) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.remove(key);
        editor.apply();
    }

    public void remove(String[] keys) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        for (String key : keys) {
            editor.remove(key);
        }
        editor.apply();
    }

    public String getString(String key) {
        return getString(key, null);
    }

    public String getString(String key, String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    public int getInt(String key, int defaultValue) {
        return mSharedPreferences.getInt(key, defaultValue);
    }

    public int getInt(String key) {
        return getInt(key, -1);
    }

    public float getFloat(String key, float defaultValue) {
        return mSharedPreferences.getFloat(key, defaultValue);
    }

    public float getFloat(String key) {
        return getFloat(key, -1);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

}
